<?php
declare (strict_types = 1);
namespace app\course\model\video;

use app\common\logic\Attach;
use app\common\model\BaseModel;
use think\facade\Request;
use think\model\relation\BelongsTo;

/**
 * 定义点播课时数据模型
 * @method join(string $string, string $string1)
 */
class CourseSectionVideo extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'course_section_video';

    /**
     * 额外填充字段
     * @var array
     */
    protected $append = ['data_type_text', 'attach'];
    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    /**
     * 不写入创建时间
     * @var boolean
     */
    protected $createTime = false;
    /**
     * 隐藏不必要的字段
     * @var array
     */
    protected $hidden = ['update_time'];

    /**
     * 课时类型
     * @var [type]
     */
    protected $dataType = [
        1 => '视频',
        2 => '音频',
        3 => '图文',
        4 => '电子书',
        // 5 => '试卷',
        // 5 => '链接',
    ];

    /**
     * 课程资源类型描述文字
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-30
     *
     * @param string $value [description]
     * @param    [type]                         $data  [description]
     * @return string [type]                                [description]
     */
    public function getDataTypeTextAttr($value = '', $data)
    {
        if (!$data) {
            return '未知';
        }

        return $this->dataType[$data['data_type']] ?? '未知';
    }

    public function getDataTxtUrlAttr($value='', $data)
    {
        return Request::domain() . '/course/section_data/' . $data['id'] . '/data_txt';
    }

    /**
     * 关联的附件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-30
     *
     * @return BelongsTo [type]                         [description]
     */
    public function attach()
    {
        // return $this->belongsTo(Attach::class, 'attach_id')->bind(['stock', 'filename','filesize','extension','duration']);
        return $this->belongsTo(Attach::class, 'attach_id')->append(['fileurl'])->bind(['stock', 'filename','filesize','extension','duration','fileurl']);
    }

    /**
     * 关联的附件基础信息【名称、URL】
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-30
     *
     * @return BelongsTo [type]                         [description]
     */
    public function attachBase()
    {
        return $this->belongsTo(Attach::class, 'attach_id')->bind(['filename','fileurl','extension']);
    }

    /**
     * 关联的附件基础信息【名称、URL】
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-30
     *
     * @return BelongsTo [type]                         [description]
     */
    public function attachSavename()
    {
        return $this->belongsTo(Attach::class, 'attach_id')->bind(['savename']);
    }

    /**
     * 获取课时数据类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-09
     *
     * @return array [type]                         [description]
     */
    public function getDataTypeOptions()
    {
        $options = [];
        foreach ($this->dataType as $key => $title) {
            $options[] = ['value' => $key, 'title' => $title];
        }

        return $options;
    }

    public function AttachStock()
    {
        return $this->belongsTo(Attach::class, 'attach_id')->bind(['stock']);
    }
}
